﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Practices.Unity;
using Praxis.Entities;
using Praxis.BusinessLogic;

namespace Praxis.UI
{
    public partial class FrmObra : Form
    {

        [Dependency]
        public IObraBL obraBL { get; set; }

        public FrmObra()
        {
            InitializeComponent();
            this.BuildUp();
        }

        private void BtnNuevo_Click(object sender, EventArgs e)
        {
            FrmMttoObra frmMttObra = new FrmMttoObra();
            frmMttObra.Nuevo();
            frmMttObra.ShowDialog();
        }

        private void FrmObra_Load(object sender, EventArgs e)
        {
            oBRABindingSource.DataSource = obraBL.GetAllObras();
        }

        private void BtnEditar_Click(object sender, EventArgs e)
        {
            OBRA currentObra = oBRABindingSource.Current as OBRA;
            if (currentObra != null)
            {
                FrmMttoObra frmMttObra = new FrmMttoObra();
                frmMttObra.CurrentObra = currentObra;
                frmMttObra.Editar();
                frmMttObra.ShowDialog();
            }
            else
            {
                MessageBox.Show("Mensaje", "Seleccione un Item", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void BtnBuscar_Click(object sender, EventArgs e)
        {
            if (rbNombre.Checked)
                oBRABindingSource.DataSource = obraBL.GetFromObraByDescripcion(TxtDescripcion.Text);

            else
                if (rbEmpresa.Checked)
                    oBRABindingSource.DataSource = obraBL.GetFromObrasByEmpresa(TxtDescripcion.Text);
        }

        private void TxtDescripcion_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (rbNombre.Checked)
                    oBRABindingSource.DataSource = obraBL.GetFromObraByDescripcion(TxtDescripcion.Text);

                else
                    if (rbEmpresa.Checked)
                        oBRABindingSource.DataSource = obraBL.GetFromObrasByEmpresa(TxtDescripcion.Text);
            }
        }

        public OBRA CurrentObra { get; set; }

        private void oBRADataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            CurrentObra = oBRABindingSource.Current as OBRA;
        }

        private void oBRADataGridView_DoubleClick(object sender, EventArgs e)
        {
            this.Close();
        }

    }
}
